<template>
  <CellBox title="包装信息">
    <FlexBox :list="4" :margin="isText ? 1 : null">
      <ElFormItemBox label="产品样式" :prop="`productStyle`">
        <ElSelectBox :data="data" dataName="productStyle" :options="proxy.$optionsArr.productStyle" :isText="isText"
          disabled />
      </ElFormItemBox>
      <ElFormItemBox :label="`每${productStyle[data.productStyle] || '枚'}${data?.productUnit || '枚'}数`"
        :prop="`pageEqualTags`">
        <ElNumberBox :data="data" dataName="pageEqualTags" :unit="data?.productUnit || '枚'" :isText="isText"
          :disabled="!isEdit" @changeBtn="setEachWeight(data)" />
      </ElFormItemBox>
      <ElFormItemBox :label="`每包${productStyle[data.productStyle] || '张'}数`" :prop="`packageEqualPages`">
        <template v-if="[2, 4].includes(data.productStyle)">
          <ElNumberBox :data="1" :unit="productStyle[data.productStyle]" :disabled="true" />
        </template>
        <ElNumberBox :data="data" dataName="packageEqualPages" :unit="productStyle[data.productStyle]"
          :disabled="!isEdit" @changeBtn="setEachWeight(data)" :isText="isText" v-else />
      </ElFormItemBox>
      <ElFormItemBox
        :label="`每箱${Number(data.packageEqualPages) > 0 || [1, 3].includes(data.productStyle) ? '包' : '卷'}数`"
        :prop="`eachBoxQuantity`">
        <ElNumberBox :data="data" dataName="eachBoxQuantity"
          :unit="Number(data.packageEqualPages) > 0 || [1, 3].includes(data.productStyle) ? '包' : '卷'"
          :disabled="!isEdit" @changeBtn="setEachWeight(data)" :isText="isText" />
      </ElFormItemBox>
      <ElFormItemBox :label="`整箱${data?.productUnit || '枚'}数`" :prop="`pageEqualTags`">
        <ElNumberBox :data="quantityBox(data)" :unit="data?.productUnit || '枚'" :isText="isText" />
      </ElFormItemBox>
      <template v-if="isPrint">
        <ElFormItemBox :label="`整箱数量`" :prop="`fullBoxQuantity`">
          <ElNumberBox :data="data" dataName="fullBoxQuantity" :disabled="!isEdit" :isText="isText" />
        </ElFormItemBox>
        <ElFormItemBox :label="`整箱规则`" :prop="`fullBoxSpecification`">
          <ElInputBox :data="data" dataName="fullBoxSpecification" :disabled="!isEdit" :isText="isText" />
        </ElFormItemBox>
        <ElFormItemBox :label="`散箱数量`" :prop="`looseBoxQuantity`">
          <ElNumberBox :data="data" dataName="looseBoxQuantity" :disabled="!isEdit" :isText="isText" />
        </ElFormItemBox>
        <ElFormItemBox :label="`散箱规则`" :prop="`looseBoxSpecification`">
          <ElNumberBox :data="data" dataName="looseBoxSpecification" :disabled="!isEdit" :isText="isText" />
        </ElFormItemBox>
        <ElFormItemBox :label="`检验员`" :prop="`inspector`">
          <ElSelectBox :data="data" dataName="inspector" :options="options.personnel" :disabled="!isEdit"
            :isText="isText" />
        </ElFormItemBox>
        <ElFormItemBox :label="`分条员`" :prop="`divisioner`">
          <ElSelectBox :data="data" dataName="divisioner" :options="options.personnel" :disabled="!isEdit"
            :isText="isText" />
        </ElFormItemBox>
      </template>
      <template v-else>
        <ElFormItemBox :label="`纸箱规格`" :prop="`cartonConfigCode`">
          <ElSelectBox :data="data" dataName="cartonConfigCode" :options="options.paperBox" @changeBtn="(value: any, row: any, arr: any) => {
            row.cartonWeight = arr[0]?.cartonWeight
            setEachWeight(data)
          }" :disabled="!isEdit" :isText="isText" />
        </ElFormItemBox>
        <ElFormItemBox :label="`纸箱重量`" :prop="`cartonWeight`">
          <ElNumberBox :data="data" dataName="cartonWeight" unit="kg" :disabled="!isEdit" :isText="isText" />
        </ElFormItemBox>
        <ElFormItemBox :label="`每箱重量`" :prop="`eachBoxWeight`">
          <ElNumberBox :data="data" dataName="eachBoxWeight" :min="data.cartonWeight" unit="kg"
            @changeBtn="setEachWeight(data)" :disabled="!isEdit" :isText="isText" />
        </ElFormItemBox>
        <ElFormItemBox :label="`每${data?.productUnit || '枚'}重量`" :prop="`eachWeight`">
          <ElNumberBox :data="data" dataName="eachWeight" unit="kg" :disabled="!isEdit" :isText="isText" />
        </ElFormItemBox>
      </template>
      <template v-if="false">
        <ElFormItemBox :label="`包装数量`" :prop="`number`">
          <ElNumberBox :data="data" dataName="number" :isText="isText" />
        </ElFormItemBox>
        <ElFormItemBox :label="`包装描述`" :prop="``">
          <ElNumberBox :data="packageDescription(data, data.number)?.text" :isText="isText" />
        </ElFormItemBox>
        <ElFormItemBox :label="`散包数量`" :prop="``">
          <ElNumberBox :data="packageDescription(data, data.number)?.remainder" :isText="isText" />
        </ElFormItemBox>
      </template>
    </FlexBox>
  </CellBox>
</template>
<script setup lang="ts">
import { getPaperBox, getPersonnel } from '@/utils/optionsArr.ts';
import { packageDescription } from '@/utils/packageDescription.ts';
const { proxy } = getCurrentInstance() as any;
const props = withDefaults(
  defineProps<{
    data?: any;//包装信息
    isEdit?: boolean;//包装信息
    isPrint?: boolean;//是否打印
    isText?: boolean;//文本
  }>(),
  {

  },
);
const options = reactive({
  paperBox: [],//纸箱规格
  customer: [],//员工
})
const productStyle: any = {
  1: '张',
  2: '卷',
  3: '张',
  4: '卷',
}
//设置每套重量
const setEachWeight = (row: any) => {
  if (row.eachBoxNum && row.eachBoxWeight) {
    row.eachWeight = proxy.$count(`(${row.eachBoxWeight || 0}-${row.cartonWeight || 0})/${row.eachBoxNum || 1}`, 6)
  } else {
    row.eachWeight = null
  }
};
//整箱数量
const quantityBox = (row: any) => {
  if (row) {
    const expression = `${row.pageEqualTags || 0}*${[2, 4].includes(props.data.productStyle) ? 1 : row.packageEqualPages || 0}*${row.eachBoxQuantity || 0}`
    const _count = proxy.$count(expression)
    row.eachBoxNum = _count
    changeBtn(row)
    return _count
  }
  return 0
};
const emit = defineEmits<{
  //fn为父组件引入子组件是定义的子组件事件属性，可随意定义
  (event: 'changeBtn', row: any): void;
}>();
const changeBtn = (row: any) => {
  emit('changeBtn', row);
};
onMounted(() => {
  if (props.isPrint) {
    getPersonnel(options, 'personnel') //获取员工
  } else {
    getPaperBox(options, 'paperBox') //获取纸箱规格
  }
});
</script>
<style scoped lang="scss"></style>
